package dao.aplicacao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import modelos.aplicacao.CorKitModel;

import dao.BaseDao;

public class CorKitDao extends BaseDao {
	
	public ArrayList<CorKitModel> getCoresVersao(int idVersao) throws SQLException{
		
		String sql = 	"SELECT  `cores`.`id` as idCor ,  `cores`.`nome` as nomeCor ,  `cores`.`valor` as valorCor " +
						"FROM  `cores` " +
						"LEFT JOIN  `versao__cores` ON (  `versao__cores`.`id_versao` = " + idVersao + " ) " +
						"WHERE  `versao__cores`.`id_cores` =  `cores`.`id` ";
		
		PreparedStatement ps = con.prepareStatement(sql);

		ResultSet result =  ps.executeQuery();
		
		ArrayList<CorKitModel> cores = new ArrayList<CorKitModel>();
		
		int i = 0;
		
		while(result.next()){
			
			CorKitModel cor = new CorKitModel();
			
			cor.setIdCor(result.getInt("idCor"));
			cor.setNomeCor(result.getString("nomeCor"));
			cor.setValorCor(result.getString("valorCor"));
			
			if(i == 0)
				cor.setChecked("checked");
			else
				cor.setChecked("");
			
			i++;
			
			cores.add(cor);
		}
		
		return cores;
	}
	
	public ArrayList<CorKitModel> getKitVersao(int idVersao) throws SQLException{
		
		String sql = 	"SELECT `id`, `nome`, `id_acessorios_1`, `id_acessorios_2`,`id_acessorios_3`,`id_acessorios_4`,`id_acessorios_5`, `preco` FROM `kit` WHERE `id_versao` = " + idVersao ;
		
		PreparedStatement ps = con.prepareStatement(sql);

		ResultSet result =  ps.executeQuery();
		
		ArrayList<CorKitModel> kits = new ArrayList<CorKitModel>();
		
		int i = 0;
		
		while(result.next()){
			
			CorKitModel kit = new CorKitModel();
			
			kit.setIdKit(result.getInt("id"));
			kit.setNomeKit(result.getString("nome"));
			kit.setIdAcessorios1(result.getInt("id_acessorios_1"));
			kit.setNomeAcessorios1(this.getAcessorioKit(kit.getIdAcessorios1()));
			kit.setIdAcessorios2(result.getInt("id_acessorios_2"));
			kit.setNomeAcessorios2(this.getAcessorioKit(kit.getIdAcessorios2()));
			kit.setIdAcessorios3(result.getInt("id_acessorios_3"));
			kit.setNomeAcessorios3(this.getAcessorioKit(kit.getIdAcessorios3()));
			kit.setIdAcessorios4(result.getInt("id_acessorios_4"));
			kit.setNomeAcessorios4(this.getAcessorioKit(kit.getIdAcessorios4()));
			kit.setIdAcessorios5(result.getInt("id_acessorios_5"));
			kit.setNomeAcessorios5(this.getAcessorioKit(kit.getIdAcessorios5()));
			kit.setPrecoKit(result.getDouble("preco"));
			
			if(i == 0)
				kit.setChecked("checked");
			else
				kit.setChecked("");
			
			i++;
			
			kits.add(kit);
		}
		
		return kits;
	}
	
	public String getAcessorioKit(int idAcessorio) throws SQLException{
		
		String sql = 	"SELECT `nome` FROM `acessorios` WHERE `id` = " + idAcessorio ;
		
		PreparedStatement ps = con.prepareStatement(sql);

		ResultSet result =  ps.executeQuery();
		
		String nomeAcessorio = "";
		
		while(result.next())
			nomeAcessorio = result.getString("nome");			
		
		return nomeAcessorio;
	}
}
